package top.happylaishop.shop.controller.fontend;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import top.happylaishop.shop.common.Const;
import top.happylaishop.shop.dto.CurrentUserDto;
import top.happylaishop.shop.dto.UserDto;
import top.happylaishop.shop.dto.UserPasswordRestDto;
import top.happylaishop.shop.service.CommonService;
import top.happylaishop.shop.service.UserService;
import top.happylaishop.shop.utils.CommonUtils;
import top.happylaishop.shop.utils.resp.BaseResponse;
import top.happylaishop.shop.utils.resp.RestResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CommonService commonService;

    /**
     * 用户登录
     * @param userDto
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<CurrentUserDto> login(@RequestBody UserDto userDto){
        userDto.setPassword(DigestUtils.md5DigestAsHex(userDto.getPassword().getBytes()));
        return userService.login(userDto);
    }

    @RequestMapping(value = "logout",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse logout(){
        redisTemplate.delete(CommonUtils.getCacheUserKey());
        return BaseResponse.success();
    }

    @RequestMapping(value = "register",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse register(@RequestBody UserDto userDto){
        userDto.setPassword(DigestUtils.md5DigestAsHex(userDto.getPassword().getBytes()));
        return userService.register(userDto);
    }

    @RequestMapping(value = "checkValid",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse checkValid(String str, String type){
        return userService.checkValid(str,type);
    }

    @RequestMapping(value = "getUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<CurrentUserDto> getUserInfo(){
        CurrentUserDto currentUserDto = commonService.getCurrentUser();
        if(currentUserDto == null){
            return RestResponse.DEFAULT_NOT_LOGIN;
        }
        return RestResponse.defaultSucessData(currentUserDto);
    }

    @RequestMapping(value = "forgetGetQuestion",method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<String> forgetGetQuestion(@RequestBody UserPasswordRestDto userPasswordRestDto){
        return RestResponse.defaultSucessData(userService.getQuestionByUsername(userPasswordRestDto.getUsername()));
    }

    @RequestMapping(value = "forgetCheckAnswer",method = RequestMethod.POST)
    @ResponseBody
    public RestResponse forgetCheckAnswer(@RequestBody UserDto userDto){
        BaseResponse baseResponse = userService.checkAnswer(userDto.getUsername(), userDto.getQuestion(), userDto.getAnswer());
        if(baseResponse.isSuccess()){
            String token = IdUtil.simpleUUID();
            redisTemplate.opsForValue().set(Const.TOKEN_FORGET_USERNAME + userDto.getUsername(), token);
            return RestResponse.defaultSucessData(token);
        }
        return RestResponse.failed(baseResponse.getMessage());
    }

    @RequestMapping(value = "forgetResetPassword",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse forgetRestPassword(@RequestBody UserPasswordRestDto userPasswordRestDto){
        String token = redisTemplate.opsForValue().get(Const.TOKEN_FORGET_USERNAME + userPasswordRestDto.getUsername());
        if(StrUtil.equals(token, userPasswordRestDto.getForgetToken())){
            userPasswordRestDto.setPasswordNew(DigestUtils.md5DigestAsHex(userPasswordRestDto.getPasswordNew().getBytes()));
            return userService.forgetRestPassword(userPasswordRestDto.getUsername(), userPasswordRestDto.getPasswordNew());
        } else {
            return BaseResponse.failed(Const.ERROR_MSG_LONG_TIME_NOT_OPERATION);
        }
    }

    @RequestMapping(value = "resetPassword",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse resetPassword(@RequestBody UserPasswordRestDto userPasswordRestDto){
        userPasswordRestDto.setPasswordNew(DigestUtils.md5DigestAsHex(userPasswordRestDto.getPasswordNew().getBytes()));
        userPasswordRestDto.setPasswordOld(DigestUtils.md5DigestAsHex(userPasswordRestDto.getPasswordOld().getBytes()));
        return userService.resetPassword(userPasswordRestDto.getPasswordOld(), userPasswordRestDto.getPasswordNew(), commonService.getCurrentUserInfo());
    }

    @RequestMapping(value = "updateInformation",method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<UserDto> updateInformation(@RequestBody UserDto userDto){
        userDto.setId(commonService.getCurrentUserInfo().getId());
        return userService.updateInformation(userDto);
    }
}
